<template>
  <div class="container">
    <div style="padding: 10px 20px;">
      <NormalMaterial :currentTab="currentTab" :tabList="tabList" :formList="formList" :queryForm="queryForm" pagePwd="Link" @tabClick="tabClick" @handleQuery="handleQuery" @handleReset="handleReset">
        <template slot="one">
          <el-table v-loading="loading" :data="companyTableList" style="width: 100%" height="400" :header-cell-style="headerCellStyle" :row-style="rowStyle" :cell-style="cellStyle">
            <el-table-column type="index" width="55" align="center" label="序号" />
            <el-table-column label="链接标题" prop="createTime">
              <template slot-scope="{ row }">
                <div style="color: #082aff; cursor: pointer" @click="toInfo(row)" v-has-permi="['admin:linkmanage:getDetail']">
                  {{ row.title }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="标签" align="center">
              <template slot-scope="scope">
                <tagGroupShowTable :tagGroup="scope.row.tagVOS" />
              </template>
            </el-table-column>
            <el-table-column label="阅读次数" prop="browseNum"></el-table-column>
            <el-table-column label="发送总次数" prop="shareNum"></el-table-column>
            <el-table-column label="创建人" prop="createBy"></el-table-column>
            <el-table-column label="创建时间" prop="updateTime">
            </el-table-column>
            <el-table-column label="操作" align="center" width="180">
              <template slot-scope="{ row }">
                <el-button type="text" @click="share()" class="tag-copy" :data-clipboard-text="clipboardText + row.id">分享</el-button>
                <el-button type="text" @click="edit(row, 'one')" v-hasPermi="['wecom:material:edit:corp']">编辑</el-button>
                <el-button type="text" @click="del(row)" v-hasPermi="['wecom:material:delete:corp']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <NormalPagination :currentPage="companyPageNum" :pageSize="companyPageSize" :total="companyTotal"
            @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange"></NormalPagination>
        </template>
        <template slot="two">
          <div style="text-align: right; padding: 20px 0; border-top: 1px solid #eee;">
            <el-button icon="el-icon-plus" type="primary" @click="toArticle()" v-has-permi="['admin:linkmanage:addLinkMaterial']">创建链接</el-button>
          </div>
          <el-table v-loading="loading" :data="selfTableList" style="width: 100%" height="400" :header-cell-style="headerCellStyle" :row-style="rowStyle" :cell-style="cellStyle">
            <el-table-column type="index" width="55" align="center" label="序号" />
            <el-table-column label="链接标题" prop="createTime">
              <template slot-scope="{ row }">
                <div style="color: #082aff; cursor: pointer" @click="toInfo(row)" v-has-permi="['admin:linkmanage:getDetail']">
                  {{ row.title }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="标签" prop="tagVOS">
              <template slot-scope="scope">
                <tagGroupShowTable :tagGroup="scope.row.tagVOS" />
              </template>
            </el-table-column>
            <el-table-column label="阅读次数" prop="browseNum"></el-table-column>
            <el-table-column label="发送总次数" prop="shareNum"></el-table-column>
            <el-table-column label="创建人" prop="createBy"></el-table-column>
            <el-table-column label="创建时间" prop="updateTime">
            </el-table-column>
            <el-table-column label="操作" align="center" width="180">
              <template slot-scope="{ row }">
                <el-button type="text" @click="share()" class="tag-copy" :data-clipboard-text="clipboardText + row.id">分享
                </el-button>
                <el-button type="text" @click="edit(row, 'one')" v-hasPermi="['wecom:material:edit']">编辑
                </el-button>
                <el-button type="text" @click="del(row)" v-hasPermi="['wecom:material:delete']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <NormalPagination :currentPage="selfPageNum" :pageSize="selfPageSize" :total="selfTotal"
            @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange">
          </NormalPagination>
        </template>
      </NormalMaterial>
    </div>
  </div>
</template>

<script>
import { articleDel, articleDelCorp, getLinkmanage } from '@/api/article/group'
import { dateFormat } from '@/utils'

export default {
  data() {
    return {
      clipboardText: window.globalConfig.BASE_API + '/scrm-api/wecom/material/open/',
      tabList: [
        { label: '企业链接库', vlaue: '0', slotName: 'one' },
        { label: '我上传的链接', vlaue: '1', slotName: 'two' },
      ],
      queryForm: {
        materialName: '',
        deptId: null,
        createBy: '',
      },
      companyForm: {
        materialName: '',
        deptId: null,
        createBy: '',
      },
      selfForm: {
        materialName: '',
        deptId: null,
        createBy: '',
      },
      formList: [
        {
          label: '链接名称',
          value: '',
          prop: 'materialName',
          type: 'INPUT',
          emunName: '',
        },
        {
          label: '管理机构',
          value: null,
          prop: 'deptId',
          type: 'MANAGE_COM',
          emunName: '',
        },
        {
          label: '创建人',
          value: '',
          prop: 'createBy',
          type: 'STAFF',
          emunName: '',
        },
      ],
      currentTab: '0',
      loading: false,
      companyTableList: [],
      companyTotal: 0,
      companyPageNum: 1,
      companyPageSize: 20,
      selfTableList: [],
      selfTotal: 0,
      selfPageNum: 1,
      selfPageSize: 20,
      headerCellStyle: {
        background: '#F8F8F8',
        color: '#1D1D1D',
        height: '40px',
        textAlign: 'center',
        borderLeft: 'none',
        borderRight: 'none',
      },
      rowStyle: {
        height: '40px',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
        whiteSpace: 'nowrap',
      },
      cellStyle: {
        height: '40px',
        padding: '0',
        textAlign: 'center',
        borderLeft: 'none',
        borderRight: 'none',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
        whiteSpace: 'nowrap',
      },
    }
  },
  created() {
    this.currentTab = this.$route.query.is_person ? this.$route.query.is_person + '' : '0'
    this.handleQuery()
  },
  methods: {
    tabClick(val) {
      this.categoryId = ''
      this.currentTab = val
      this.handleReset()
      // if (this.currentTab == '0') {
      //   this.queryForm = this.companyForm
      // } else {
      //   this.queryForm = this.selfForm
      // }
      // this.handleQuery(this.categoryId, 1)
    },
    handleQuery(categoryId, pageNum) {
      this.loading = true
      this.categoryId = categoryId
      let tempNum = this.currentTab == '0' ? this.companyPageNum : this.selfPageNum
      let tempSize = this.currentTab == '0' ? this.companyPageSize : this.selfPageSize
      let params = {
        pageNum: pageNum || tempNum,
        pageSize: tempSize,
        mediaType: 11,
        levelType: +this.currentTab,
        categoryId: this.categoryId,
        materialName: this.queryForm.materialName,
        deptId: this.queryForm.deptId,
        createBy: this.queryForm.createBy + '',
      }
      getLinkmanage(params).then((res) => {
        this.loading = false
        if (res.code === 200) {
          const { rows, total } = res
          if (this.currentTab == '0') {
            this.companyForm = this.queryForm
            this.companyTableList = rows || []
            this.companyTotal = total || 0
          } else {
            this.selfForm = this.queryForm
            this.selfTableList = rows || []
            this.selfTotal = total || 0
          }
        }
      })
    },
    handleReset() {
      this.queryForm = {
        materialName: '',
        deptId: null,
        createBy: ''
      }
      if (this.currentTab == '0') {
        this.companyForm = this.queryForm
      } else {
        this.selfForm = this.queryForm
      }
      this.handleQuery(this.categoryId, 1)
    },
    handleSizeChange(val) {
      if (this.currentTab == '0') {
        this.companyPageNum = 1
        this.companyPageSize = val
      } else {
        this.selfPageNum = 1
        this.selfPageSize = val
      }
      this.handleQuery(this.categoryId)
    },
    handleCurrentChange(val) {
      if (this.currentTab == '0') {
        this.companyPageNum = val
      } else {
        this.selfPageNum = val
      }
      this.handleQuery(this.categoryId)
    },
    edit(row, name) {
      this.$router.push({
        name: 'LinkManage/edit',
        query: {
          name,
          id: row.id,
          levelType: +this.currentTab,
          mediaType: 11,
          is_person: +this.currentTab,
          operate: 'EDIT'
        },
      })
    },
    del(row) {
      this.$confirm('确认删除此链接信息', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          if (this.currentTab == '0') {
            articleDelCorp(row.id).then((res) => {
              if (res.code === 200) {
                this.$message.success(res.msg)
                this.handleQuery(this.categoryId)
              }
            })
          } else if (this.currentTab == '1') {
            articleDel(row.id).then((res) => {
              if (res.code === 200) {
                this.$message.success(res.msg)
                this.handleQuery(this.categoryId)
              }
            })
          }
        })
        .catch(() => {
          this.loading = false
        })
    },
    share() {
      let clipboard = new this.clipboard('.tag-copy')
      clipboard.on('success', (e) => {
        this.$message({
          message: '复制链接成功!',
          type: 'success',
          duration: 2000,
        })
        //清除选中
        e.clearSelection()
        //释放内存，以防重复复制
        clipboard.destroy()
      })
      clipboard.on('error', (e) => {
        // 不支持复制
        this.$message({
          message: '复制链接失败!',
          type: 'error',
          duration: 2000,
        })
        //清除选中
        e.clearSelection()
        //释放内存，以防重复复制
        clipboard.destroy()
      })
    },
    toArticle() {
      this.$router.push({
        name: 'LinkManage/edit',
        query: { is_person: 1, operate: 'ADD' },
      })
    },
    toInfo(row) {
      this.$router.push({
        name: 'LinkManage/edit',
        query: {
          id: row.id,
          levelType: +this.currentTab,
          mediaType: 11,
          is_person: +this.currentTab,
          operate: 'DETAIL'
        },
      })
    },
    dateFormat,
  },
}
</script>
<style lang="scss" scoped>
.container {
  height: 100%;
  background: #fff;
}
</style>
